import Koa from 'koa';
import koaBody from 'koa-body';
import compress from 'koa-compress';
import staticServer from 'koa-static';
import router from './routes/index';
import config from '../config';

import './utils/logger';

const app = new Koa();

app.use(
  compress({
    filter(content_type: any) {
      return /text/i.test(content_type);
    },
    threshold: 2048,
    gzip: {
      flush: require('zlib').constants.Z_SYNC_FLUSH,
    },
    deflate: {
      flush: require('zlib').constants.Z_SYNC_FLUSH,
    },
    br: false, // disable brotli
  }),
);

app.use(koaBody());
app.use(staticServer(__dirname + '/../dist'));

app.use(async (ctx: any, next: any) => {
  ctx.set('Content-Type', 'application/json');
  await next();
});

app.use(router.routes());

app.listen(config.port, () => {
  console.log(`应用已经启动：http://localhost:${config.port}`);
});
